package com.liumapp.auth.zuul.personal.api.config;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.ApiKeyVehicle;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.PostConstruct;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${security.oauth2.client.client-id}")
    private String authClientId;

    @Value("${security.oauth2.client.client-secret}")
    private String authClientSecret;

    @Value("${security.oauth2.resource.token-info-uri}")
    private String tokenInfoUrl;

    @Value("${security.oauth2.client.access-token-uri}")
    private String accessTokenUri;

    @Value("${security.oauth2.client.user-authorization-uri}")
    private String authorizationUrl;

    private static final String TYPE = "oauth2";

    @Bean
    public Docket createRestApi(){

        /***
         * 设置header 属性
         *
        ParameterBuilder uid = new ParameterBuilder();
        uid.name("access_token").description("access_token").modelRef(new ModelRef("string")).parameterType("query").required(true).build();

        List<Parameter> pars = Lists.newArrayList(uid.build()) ;
        */

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("OpenApi Platform API Document")
                        .description("spring boot restful")
                        .version("1.0")
                        .contact(new Contact("laiyujun", "https://blog.csdn.net/Ryan200909", "laiyujun@vip.qq.com"))
                        .build()
                )
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.liumapp.auth.zuul.personal.api.controller"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Lists.newArrayList(oauth()))
                .securityContexts(Lists.newArrayList(securityContext()));
//                .globalOperationParameters(pars);
    }

//    @Bean
    private SecurityContext securityContext() {
        AuthorizationScope[] scopes = new AuthorizationScope[]{new AuthorizationScope("read", "读")};

        SecurityReference securityReference = SecurityReference.builder()
                .reference(TYPE)
                .scopes(scopes)
                .build();

        return SecurityContext.builder()
                .securityReferences(Lists.newArrayList(securityReference))
                .forPaths(PathSelectors.any()) //ant("/api/**") // 最好是能够统一前缀
                .build();
    }

//    @Bean
    private SecurityScheme oauth() {
        return new OAuthBuilder()
                .name(TYPE)
                .grantTypes(grantTypes())
                .scopes(scopes())
                .build();
    }

    private List<AuthorizationScope> scopes() {
        return Lists.newArrayList(new AuthorizationScope("read", "读取"),
                new AuthorizationScope("write", "写入")
        );
    }

    private List<GrantType> grantTypes() {
        return Lists.newArrayList(
                new AuthorizationCodeGrant(
                    new TokenRequestEndpoint(authorizationUrl, authClientId, authClientSecret),
                    new TokenEndpoint(accessTokenUri, "access_token")
                )
        );
    }

    @Bean
    public SecurityConfiguration securityInfo() {
        return new SecurityConfiguration(authClientId, authClientSecret,
                "", "",
                "", ApiKeyVehicle.HEADER, "", "");
    }
}
